package cn.dansj.service.Impl;

import cn.dansj.common.utils.json.DictMap;
import cn.dansj.entity.Menu;
import cn.dansj.repository.MenuRepository;
import cn.dansj.service.MenuService;
import cn.dansj.common.redis.RedisUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {
    private final MenuRepository menuRepository;
    private final RedisUtils redisUtils;

    MenuServiceImpl(MenuRepository menuRepository, RedisUtils redisUtils) {
        this.menuRepository = menuRepository;
        this.redisUtils = redisUtils;
    }

    public List<Menu> getMenu() {
        final List<Menu> menu = menuRepository.getMenu();
        redisUtils.set("menu", DictMap.toJSONString(menu));
        return menu;
    }

    @Override
    public void save(Menu menu) {
        menuRepository.save(menu);
    }

    @Override
    public void save(List<Menu> menuList) {
        menuRepository.saveAll(menuList);
    }

    @Override
    public void delete() {
        menuRepository.deleteAllInBatch();
    }

    @Override
    public void delMenu(Menu menu) {
        menuRepository.delete(menu);
    }

    @Override
    public List<Menu> getRootMenu() {
        return menuRepository.getAllRootMenu();
    }

    @Override
    public List<Menu> getRoleMenu(String role) {
        return menuRepository.getRoleMenu(role);
    }
}
